<?php
namespace Home\Controller\SuperUser;
use Think\Controller;
/*
 * 超级管理员 2016.5.27
 * author:candy
 */

class GoodController extends Controller {

    function _initialize(){
        // \Think\Hook::add('menu','Home\\Behaviors\\SuperUserBehavior');
    }

    public function _empty($name){
        $this->display("Event:404");      
    }


    public function index(){
    	
    }
    /**
     * 根据id 获取商品
     * 
     * @param  [type] $id [good_id]
     * @return [type]     [description]
     */
    public function getGood($id=null){
        if($id){
            $Good = M('Good');
            $good_result = $Good->distinct(true)->group('good.good_id')
            ->join('item ON good.good_id = item.good_id','LEFT')
            ->join('picture ON picture.id = good.good_id','LEFT')
            ->order('price desc')
            ->field('good.name as gname,item.name as iname,price,picture.pic_name,picture.pic_path,place,expressplace,picture.type,good.good_id,item.item_id')
            ->where('good.good_id="'.$id.'"')->select();
            dump($good_result);
        }
    }

    public function search($keyword=null){
        if($keyword){
            $map['good.name'] = array('like','%'.$keyword.'%');
            $Good = M('Good');
            $good_result = $Good->distinct(true)->group('good.good_id')
            ->join('item ON good.good_id = item.good_id','LEFT')
            ->join('picture ON picture.id = good.good_id','LEFT')
            ->order('price desc')
            ->field('good.name as gname,item.name as iname,price,picture.pic_name,picture.pic_path,place,expressplace,picture.type,good.good_id,item.item_id,status')
            ->where($map)->select();
            dump($good_result);
        }
    }
    /**
     * 6.18
     * @param  [type] $id [good_id]
     * @param  [type] $status [status]
     * 修改商品状态
     */
    public function ModifyGood($id=null,$status=null){
        if($id&&$status){
            $Good = M('Good');
            $Good->status= $status;
            echo $Good->where('good.good_id="'.$id.'"')->save();
        }
        $this->getDetail($id);
    }

    /**
     * 7.10
     * 商品分类管理
     */
    public function goodType(){
        $type = M('Goodtype');
        $dataList = array();
        if(POST){
            for ($i=0; $i < count($_POST['maintype']); $i++) { 
                $subtypes = split(',',$_POST['subtype'][$i]);
                if($_POST['maintype'][$i]){
                    $type->where("maintype != ''")->delete();
                    for ($j=0; $j < count($subtypes); $j++) { 
                        $dataList[] = array(
                            'maintype'=>$_POST['maintype'][$i],
                            'subtype'=>$subtypes[$j]
                        );
                    }
                }
            }
            // dump($dataList);
            $type->addAll($dataList);
        }
        $result = $type->select();

        $types = array();
        $current;
        $type = array();
        if(count($result)>0){

          foreach ($result as $key => $item) {
            if(!$current||$item['maintype']==$current['maintype']){
              array_push($type,$item['subtype']);
              $current = $item;
            }else{
              array_push($types,array('maintype'=>$current['maintype'],'items'=>implode(',',$type)));
              $type = array();
              array_push($type,$item['subtype']);
              $current = $item;
            }
          }
          array_push($types,array('maintype'=>$current['maintype'],'items'=>implode(',',$type)));
          $this->assign('types',$types);
        }
        // $this->assign('list',$result);
        $this->display('SuperUser/good/goodtype');
    }


    /**
     * 6.5
     * 商品查询
     * 选取规格中价格最低作为标价，选取第一张图片作为图片
     这里需要多测试，多联合查询容易出错
     */
    public function listgood(){
        $Good = M('Good');
        $result = $Good->distinct(true)->group('good.good_id')
        ->join('item ON good.good_id = item.good_id','LEFT')
        ->join('picture ON picture.id = good.good_id','LEFT')
        ->join('shop ON shop.shop_id = good.shop_id','LEFT')
        ->order('price desc')
        ->field('good.name as gname,price,picture.pic_name,picture.pic_path,good.place,expressplace,picture.type,good.good_id,item.item_id,shop.name as sname,good.description')
        ->select();
        // dump($result);
        $this->assign('list',$result);
        $this->display('SuperUser/good/manage');
    }

    /**
     * 6.14
     * 商品详情
     */
    public function getDetail($id=null){
        $Good = M('Good');
        $result_item = $Good->distinct(true)
        ->join('item I ON good.good_id = I.good_id','LEFT')
        ->where('good.good_id='.$id)
        ->field('I.name as iname,good.name as gname,price,place,expressplace,remain,description,type,subtype,status,good.good_id')
        ->order('price desc')
        ->find();
        dump($result_item);
        $this->assign('good',$result_item);
        $Pic = M('Picture');
        $result_pic = $Pic->distinct(true)
        ->where('id='.$id)
        ->select();
        dump($result_pic);
        $this->display('SuperUser/good/detail');
    }

    /**
     * 7.10
     * 商品管理
     */
    public function manage(){
      $this->listgood();
    }

    /**
     * 8.27
     * 投诉商品管理
     * 0 待处理  1 已处理
     */
    public function arguelist($status=0){
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         * Easy set variables
         */
         //status 6 代表争议订单
        // DB table to use
        $table ='complaint_good';
         
        // Table's primary key
        $primaryKey = 'id';
         
        // Array of database columns which should be read and sent back to DataTables.
        // The `db` parameter represents the column name in the database, while the `dt`
        // parameter represents the DataTables column identifier. In this case object
        // parameter names
        $columns = array(
            array( 'db' => 'good_name', 'dt' => 'gname' , 'field' => 'gname','as'=>'gname'),
            array( 'db' => 'shop_name', 'dt' => 'sname' , 'field' => 'sname','as'=>'sname'),
            array( 'db' => 'user_name', 'dt' => 'uname' , 'field' => 'uname','as'=>'uname'),
            array( 'db' => 'good_id',  'dt' => 'gid' ,  'field' => 'gid','as'=>'gid'),
            array( 'db' => 'user_id',  'dt' => 'uid' ,  'field' => 'uid','as'=>'uid'),
            array( 'db' => 'shop_id',  'dt' => 'sid' ,  'field' => 'sid','as'=>'sid'),
            array( 'db' => 'datetime',  'dt' => 'datetime' ,  'field' => 'datetime'),
            array( 'db' => 'id',  'dt' => 'id' ,  'field' => 'id'),
            array( 'db' => 'status',  'dt' => 'status' ,  'field' => 'status'),
        );
         
        // SQL server connection information
        $sql_details = array(
            'user' => C('DB_USER'),
            'pass' => C('DB_PWD'),
            'db'   => C('DB_NAME'),
            'host' => C('DB_HOST')
        );

        if(I('get.startdate')&&I('get.enddate'))
            $where = " AND `datetime` between '".I('get.startdate')."' AND '".I('get.enddate')."'";
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         * If you just want to use the basic configuration for DataTables with PHP
         * server-side, there is no need to edit below this line.
         */
        // $joinQuery = "FROM `{$table}` AS `g` LEFT JOIN `shop` AS `s` ON (`s`.`shop_id` = `o`.`shop_id`))";
        $extraCondition = $where;
        Vendor('SSP');
        echo json_encode(
               \SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, null, $extraCondition)
             );
    }

    public function tagdone($id=null,$status=0){
      if($id){
        $cg = M('complaint_good');
        $cg->status =$status;
        if($cg->where(array('id'=>$id))->save()){

        }else{

        }
      }
    }

    public function hotel(){
      $hotel = M('Hotel');
      if(IS_POST){
        date_default_timezone_set("Asia/Shanghai");
        // $data['hotel_name'] = I('post.hotel_name');
        // $data['url'] = I('post.url');
        // $data['pic_url'] = I('post.pic_url');
        // $data['datetime'] = date('y-m-d h:i:s',time());
        $hotel->create();
        $hotel->datetime =  date('y-m-d h:i:s',time());
        if($hotel->add()){

        }else{

        }
      }else{
        
      }
      $this->display("SuperUser/good/hotel");
    }    

    public function modifyhotel(){
      $hotel = M('Hotel');
      if(IS_POST){
        date_default_timezone_set("Asia/Shanghai");
        $hotel->hotel_name = I('post.hotel_name');
        $hotel->url = I('post.url');
        $hotel->pic_url = I('post.pic_url');
        // $hotel->datetime = date('y-m-d h:i:s',time());
        
        if($hotel->where('idhotel = '.I('post.id'))->save()){

        }else{

        }
      }else{
        
      }
      $this->display("SuperUser/good/hotel");
    }

    public function hoteldata(){
              /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         * Easy set variables
         */
         //status 6 代表争议订单
        // DB table to use
        $table ='hotel';
         
        // Table's primary key
        $primaryKey = 'idhotel';
         
        // Array of database columns which should be read and sent back to DataTables.
        // The `db` parameter represents the column name in the database, while the `dt`
        // parameter represents the DataTables column identifier. In this case object
        // parameter names
        $columns = array(
            array( 'db' => 'idhotel', 'dt' => 'id' , 'field' => 'id','as'=>'id'),
            array( 'db' => 'url', 'dt' => 'url' , 'field' => 'url','as'=>'url'),
            array( 'db' => 'hotel_name', 'dt' => 'name' , 'field' => 'name','as'=>'name'),
            array( 'db' => 'pic_url',  'dt' => 'pic_url' ,  'field' => 'pic_url'),
            array( 'db' => 'datetime',  'dt' => 'datetime' ,  'field' => 'datetime'),
        );
         
        // SQL server connection information
        $sql_details = array(
            'user' => C('DB_USER'),
            'pass' => C('DB_PWD'),
            'db'   => C('DB_NAME'),
            'host' => C('DB_HOST')
        );

        if(I('get.startdate')&&I('get.enddate'))
            $where = " AND `datetime` between '".I('get.startdate')."' AND '".I('get.enddate')."'";
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         * If you just want to use the basic configuration for DataTables with PHP
         * server-side, there is no need to edit below this line.
         */
        // $joinQuery = "FROM `{$table}` AS `g` LEFT JOIN `shop` AS `s` ON (`s`.`shop_id` = `o`.`shop_id`))";
        $extraCondition = $where;
        Vendor('SSP');
        echo json_encode(
               \SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, null, $extraCondition)
             );
    }

    public function hoteldelete($id){
      $hotel = M('hotel');
      $hotel->delete($id);
    }
}